package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.BaseAdapter;
import com.hjq.widget.layout.SettingBar;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageNewBean;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.ListLabelAdapter;
import com.qiantu.phone.ui.dialog.BottomListDialog;
import com.qiantu.phone.ui.uiutil.LLSceneLinkageUtil;
import com.qiantu.phone.utils.LinkageDataHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * time   : 2021/12/22
 */
public final class HumanPresenceInductionActivityNew extends AppActivity {
    private SettingBar settingAction;
    private SettingBar settingReferenceArea;
    private SettingBar settingDataSourceDevice;
    private SettingBar settingLinkageAction;
    private TextView tvCondition;
    private RadioGroup whenRadioGroup;
    private TextView settingTitle;
    private String deviceName;
    private LinkageNewBean mLinkageBean;
    private LinkageNewBean.LinkageDeviceCondition mLinkageCondition;
    private BottomListDialog mBottomActionDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_human_presence_induction_new;
    }

    @Override
    protected void initView() {
        mLinkageCondition =  getSerializable("condition");
        settingAction = findViewById(R.id.setting_action);
        settingReferenceArea = findViewById(R.id.setting_reference_area);
        settingReferenceArea.setEnabled(false);
        settingDataSourceDevice = findViewById(R.id.setting_data_source_device);
        settingLinkageAction = findViewById(R.id.setting_linkage_action);
        settingReferenceArea.setRightDrawable(null);
        settingDataSourceDevice.setVisibility(View.VISIBLE);
        setOnClickListener(settingDataSourceDevice,settingAction);
        tvCondition = findViewById(R.id.tv_condition);
        whenRadioGroup = findViewById(R.id.when_radio_group);
        settingTitle = findViewById(R.id.setting_title);
    }

    @Override
    protected void initData() {
        mLinkageBean = LinkageDataHolder.getLinkageDataNew();

        if (mLinkageCondition == null) {
            deviceName = getString("device_name");
            mLinkageCondition = new LinkageNewBean.LinkageDeviceCondition();
            mLinkageCondition.setConditionTypeId(getInt("environmentalType"));
            mLinkageCondition.setAreaTypeId(1);
            mLinkageCondition.setCompareTypeId(2);
            mLinkageCondition.setActionTypeId(1);

            mLinkageCondition.setDeviceSerialNo(getString("device_serial_no"));
            mLinkageCondition.setAreaName(getString("floor_room_name"));
            mLinkageCondition.setAreaCode(getString("room_serial_no"));
            mLinkageCondition.setBeginValue(1);
            mLinkageCondition.setEndValue(1);

            mLinkageCondition.setIcon(getImageName());
            mLinkageCondition.setDescription(getString(LLSceneLinkageUtil.getLinkageEnvrionmentName(getInt("environmentalType"))));
            mLinkageCondition.setStateName(LLSceneLinkageUtil.getLinkageState(getInt("environmentalType")));
            mLinkageCondition.setSort(mLinkageBean.getConditions().indexOf(mLinkageCondition) + 1);
        }else{
            DeviceBean deviceBean = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(mLinkageCondition.getDeviceSerialNo());
            if(deviceBean != null){
                deviceName = deviceBean.getName();
            }
        }
        whenRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.type_somebody:
                        mLinkageCondition.setBeginValue(1);
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(LLSceneLinkageUtil.getStateActionName(mLinkageCondition.getConditionTypeId(),(int) mLinkageCondition.getBeginValue()))));
                        break;
                    case R.id.type_nobody:
                        mLinkageCondition.setBeginValue(0);
                        tvCondition.setText(getString(R.string.effective_when_time_holder, getString(LLSceneLinkageUtil.getStateActionName(mLinkageCondition.getConditionTypeId(),(int) mLinkageCondition.getBeginValue()))));
                        break;
                }
            }
        });
        ((RadioButton)whenRadioGroup.getChildAt(0)).setChecked(true);
        updateView();
    }

    private void updateView() {
        int typeName = LLSceneLinkageUtil.getLinkageEnvrionmentName(mLinkageCondition.getConditionTypeId());
        setTitle(typeName);
        settingTitle.setText(getString(typeName) + getString(R.string.setting_title));
        RadioButton leftButton = findViewById(R.id.type_somebody);
        leftButton.setText(LLSceneLinkageUtil.getStateActionName(mLinkageCondition.getConditionTypeId(),1));
        RadioButton rightButton = findViewById(R.id.type_nobody);
        rightButton.setText(LLSceneLinkageUtil.getStateActionName(mLinkageCondition.getConditionTypeId(),0));
        List<DeviceBean> data = LLDeviceDBManager.getInstance(getContext()).getRoomEnvironmentDevices(mLinkageCondition.getAreaCode(), mLinkageCondition.getConditionTypeId());
        if(data.size() > 1){
            settingDataSourceDevice.setRightDrawable(AppApplication.getInstance().getThemeDrawable(R.attr.icon_list_next_normal));
            settingDataSourceDevice.setEnabled(true);
        }else{
            settingDataSourceDevice.setRightDrawable(null);
            settingDataSourceDevice.setEnabled(false);
        }
        settingReferenceArea.setRightText(mLinkageCondition.getAreaName());
        settingDataSourceDevice.setRightText(deviceName);
        switch ((int) mLinkageCondition.getBeginValue()) {
            case 1:
                ((RadioButton) whenRadioGroup.getChildAt(0)).setChecked(true);
                break;
            case 0:
                ((RadioButton) whenRadioGroup.getChildAt(2)).setChecked(true);
                break;
        }

        settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
    }

    @Override
    public void onRightClick(View view) {
        if(getBoolean("isAdd")){
            mLinkageBean.getConditions().add(mLinkageCondition);
        }else{
            int position = getInt("position");
            mLinkageBean.getConditions().remove(position);
            mLinkageBean.getConditions().add(position,mLinkageCondition);
        }
        startActivity_SingleTask(EditLinkageActivityNew.class);
        finish();
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.setting_action:
                showBottomListDialog();
                break;
            case R.id.setting_data_source_device:
                intent = new Intent(this, DataSourceDeviceActivity.class);
                intent.putExtra("device_serial_no", mLinkageCondition.getDeviceSerialNo());
                intent.putExtra("room_serial_no", mLinkageCondition.getAreaCode());
                intent.putExtra("environment_type", mLinkageCondition.getConditionTypeId());
                startActivityForResult(intent, new OnActivityCallback() {
                    @Override
                    public void onActivityResult(int resultCode, @Nullable Intent data) {
                        if(resultCode == RESULT_OK){
                            mLinkageCondition.setAreaName(data.getStringExtra("floor_room_name"));
                            mLinkageCondition.setAreaCode(data.getStringExtra("room_serial_no"));
                            deviceName = data.getStringExtra("device_name");
                            mLinkageCondition.setDeviceSerialNo(data.getStringExtra("device_serial_no"));
                            settingReferenceArea.setRightText(mLinkageCondition.getAreaName());
                            settingDataSourceDevice.setRightText(deviceName);
                        }
                    }
                });
                break;
        }
    }


    private String getImageName(){
        if(mLinkageCondition.getConditionTypeId() == 10){
            return "icon_intelligent_environment_pir2";
        }else if(mLinkageCondition.getConditionTypeId() == 16){
            return "icon_intelligent_environment_gatemagnetism";
        }else if(mLinkageCondition.getConditionTypeId() == 17){
            return "icon_intelligent_environment_gas";
        }else if(mLinkageCondition.getConditionTypeId() == 18){
            return "icon_intelligent_environment_smog";
        }else if(mLinkageCondition.getConditionTypeId() == 19){
            return "icon_intelligent_environment_trigger";
        }else if(mLinkageCondition.getConditionTypeId() == 20){
            return "icon_intelligent_environment_alarm";
        }else if(mLinkageCondition.getConditionTypeId() == 21){
            return "icon_intelligent_environment_infrared";
        }else if(mLinkageCondition.getConditionTypeId() == 22){
            return "icon_intelligent_environment_correlation";
        }else if(mLinkageCondition.getConditionTypeId() == 23){
            return "icon_intelligent_environment_shake";
        }else if(mLinkageCondition.getConditionTypeId() == 24){
            return "icon_intelligent_environment_curtain";
        }
        return "";
    }

    private void showBottomListDialog() {
        if (mBottomActionDialog == null) {
            mBottomActionDialog = new BottomListDialog(this);
            mBottomActionDialog.setDialogClickListener(new BottomListDialog.DialogClickListener() {
                @Override
                public void onConfirmClick() {

                }
            });
            ListLabelAdapter listLabelAdapter = new ListLabelAdapter(this);
            List<String> data = new ArrayList<>();
            data.add("变为");
            data.add("处于");
            listLabelAdapter.setData(data);
            listLabelAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                    mBottomActionDialog.dismiss();
                    mLinkageCondition.setActionTypeId(position + 1);
                    settingAction.setRightText(mLinkageCondition.getActionTypeId() == 1 ? R.string.linkage_action_become : R.string.linkage_action_keep);
                }
            });
            mBottomActionDialog.setAdapter(listLabelAdapter);
            new XPopup.Builder(this)
                    .dismissOnBackPressed(true)
                    .enableDrag(true)
                    .asCustom(mBottomActionDialog);
        }
        ListLabelAdapter listLabelAdapter = (ListLabelAdapter) mBottomActionDialog.getAdapter();
        listLabelAdapter.setSelected("");
        mBottomActionDialog.show();
    }
}